package com.ls.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {

    /**
     * 注入认证管理器 密码模式需要
     */
    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * 要让oauth的token和jwt存储做关联
     *
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private CustomTokenEnhancer customTokenEnhancer;

    /**
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("web") // 第三方应用的客户端id
                .secret(passwordEncoder().encode("web-secret")) // 配置第三方应用的密码
                .scopes("read") // 配置第三方应用的业务作用域
                .authorizedGrantTypes("authorization_code") //
                .accessTokenValiditySeconds(7200) // token的有效时间
                .redirectUris("https://www.baidu.com") // 授权后跳转的uri地址

                .and()
                .withClient("wx")
                .secret(passwordEncoder().encode("wx-secret"))
                .scopes("all")
                .authorizedGrantTypes("implicit") // 静默授权
                .accessTokenValiditySeconds(3600)
                .redirectUris("https://www.baidu.com")

                .and()
                .withClient("qq")
                .secret(passwordEncoder().encode("qq-secret"))
                .scopes("write")
                .authorizedGrantTypes("password") // 密码授权
                .accessTokenValiditySeconds(7200)
                .redirectUris("https://www.baidu.com")

                .and()
                .withClient("client")
                .secret(passwordEncoder().encode("client-secret"))
                .scopes("readOnly")
                .authorizedGrantTypes("client_credentials") // 客户端授权
                .accessTokenValiditySeconds(500)
                .redirectUris("https://www.baidu.com");
    }

    /**
     * 需要暴露授权服务给token的存储
     * 暴露授权服务器给认证管理器
     * 暴露授权服务给token转换器
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain enhancerChain=new TokenEnhancerChain();
        List<TokenEnhancer> enhancers=new ArrayList<>();
        //添加自定义jwt信息
        enhancers.add(customTokenEnhancer);
        //将access_token转换成jwt
        enhancers.add(jwtAccessTokenConverter());
        enhancerChain.setTokenEnhancers(enhancers);

        endpoints.tokenEnhancer(enhancerChain)
                .authenticationManager(authenticationManager);
    }

    /**
     * jwt的token转换器
     * 把用户信息转成jwt的形式颁发出去
     *
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //对称加密
        jwtAccessTokenConverter.setSigningKey("ls");
        return jwtAccessTokenConverter;
    }

}
